<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__static/css/public.css" media="all">
    <link rel="stylesheet" href="__static/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
        .layui-table-cell {
            height: auto;
        }
        #rules {
            width: 50%;
            max-width: 100%;
        }
    </style>
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            <p><i class="fa fa-address-book"></i> 角色管理</p>
        </blockquote>

        <script type="text/html" id="roleToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm new-role" lay-event="add"> <span class="fa fa-plus"></span> 添加新角色 </button>
            </div>
        </script>

        <table class="layui-hide" id="roleTable" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="roleTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs role-edit" lay-event="edit">编辑</a>

            <a style="{{d.id >14?'':'display:none'}}" class="layui-btn layui-btn-xs layui-btn-danger role-delete" lay-event="delete">删除</a>
        </script>

    </div>

</div>
<script src="__static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'form'], function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            layer = layui.layer;

        table.render({
            elem: '#roleTable',
            id: 'roleTable',
            url: '/system/user_role/t/json',
            toolbar: '#roleToolbar',
            defaultToolbar: [],
            cols: [[
                {field: 'id', width: 80, title: 'ID'},
                {field: 'name', width: 150, title: '角色名称'},
                {field: 'rules', title: '权限', templet: function(a) {
                    var tmpl = '<span class="{0}">{1}</span> ',
                        rules = a.rules_val,
                        br_cnt = 0,
                        str = '';
                    for (var i in rules) {
                        var bgc = 'layui-badge layui-bg-blue';
                        if (rules[i].lv == 1) {
                            if (br_cnt < 1) {
                                str += '<br>';
                                br_cnt += 1;
                            }
                            bgc = 'layui-badge layui-bg-green';
                        }
                        if (rules[i].lv == 2) {
                            if (br_cnt < 2) {
                                str += '<br>';
                                br_cnt += 1;
                            }
                            bgc = 'layui-badge-rim';
                        }
                        str += tmpl.replace('{0}', bgc).replace('{1}', rules[i].title);
                    }
                    return str;
                }},
                {title: '操作', width: 150, fixed: 'right', toolbar: '#roleTableBar', align: "center"}
            ]],
            page: false,
            skin: 'line'
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加新角色',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/role_edit.html',
                    end: function() {
                        table.reload('roleTable');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/role_edit/id/' + data.id,
                    end: function() {
                        table.reload('roleTable');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                var confirm = layer.confirm('删除角色 [' + data.name + ']？', {
                    yes: function(id) {
                        var url = '/system/role_del/id/' + data.id;
                        $.get(url, function(d) {
                            layer.msg(d.msg, {
                                time: 500
                            });
                            if (!d.code) {
                                obj.del();
                            }
                        });
                        layer.close(confirm);
                    }
                });
            }
        });

    });
</script>
</body>
</html>